package com.atguigu.chapter07.state;

import com.atguigu.bean.WaterSensor;
import com.atguigu.util.AtguiguUtil;
import org.apache.flink.api.common.state.MapState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.contrib.streaming.state.EmbeddedRocksDBStateBackend;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.io.IOException;
import java.util.List;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/7/20 11:40
 */
public class Flink09_State_Backend {
    
    public static void main(String[] args) throws IOException {
        System.setProperty("HADOOP_USER_NAME", "atguigu");
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
        
        env.enableCheckpointing(2000);
        // 旧的写法
        //env.setStateBackend(new MemoryStateBackend());  // 默认状态后端
        // 新的写法:(1.13)
        //env.setStateBackend(new HashMapStateBackend());
        //env.getCheckpointConfig().setCheckpointStorage(new JobManagerCheckpointStorage());
        
        //
        // env.setStateBackend(new FsStateBackend("hdfs://hadoop162:8020/ck"));
        
        //env.setStateBackend(new HashMapStateBackend());
       // env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop162:8020/ck");
//        env.setStateBackend(new RocksDBStateBackend("hdfs://hadoop162:8020/ck/rocks"));
        
        env.setStateBackend(new EmbeddedRocksDBStateBackend());
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop162:8020/ck/rocks");
        
        env
            .socketTextStream("hadoop162", 9999)
            .map(value -> {
                String[] datas = value.split(",");
                return new WaterSensor(datas[0], Long.valueOf(datas[1]), Integer.valueOf(datas[2]));
                
            })
            .keyBy(WaterSensor::getId)
            
            .process(new KeyedProcessFunction<String, WaterSensor, String>() {
                
                private MapState<Integer, Object> vcState;
                
                @Override
                public void open(Configuration parameters) throws Exception {
                    vcState = getRuntimeContext()
                        .getMapState(new MapStateDescriptor<Integer, Object>(
                            "vcState",
                            Integer.class,
                            Object.class)
                        );
                }
                
                @Override
                public void processElement(WaterSensor value,
                                           Context ctx,
                                           Collector<String> out) throws Exception {
                    vcState.put(value.getVc(), new Object());
                    
                    List<Integer> vcs = AtguiguUtil.toList(vcState.keys());
                    out.collect(ctx.getCurrentKey() + "  " + vcs);
                    
                }
            })
            .print();
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}

